package scsys.webServlet.main;


import com.alibaba.fastjson.JSON;
import scsys.pojo.User;
import scsys.servlet.UserService;
import scsys.servlet.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;

@WebServlet("/add_class_Servlet")
public class add_class_Servlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        UserService userService = new UserServiceImpl();
        //1. 接收数据
        BufferedReader br = request.getReader();
        String params = br.readLine();//json字符串
        //转为User对象
        User user = JSON.parseObject(params, User.class);
        if (user == null){
            response.getWriter().write("no");
            return;
        }else {
            // 获取查询对象·
            User selectUser = null;
            selectUser = userService.selectOne(user);
            if(selectUser!=null){
                //2. 转为JSON
                String jsonString = JSON.toJSONString(selectUser);
                //3. 写数据
                response.setContentType("text/json;charset=utf-8");
                response.getWriter().write(jsonString);
            }else {
                response.getWriter().write("no");
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
